﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlTypes;

namespace Quản_Lý_Thư_Viện
{
    class NhanvienDataAccess
    {
        private DataProvider _provider = new DataProvider();
        public NhanvienDataAccess()
        {
            _provider.connect();
        }
        public void insertnhanvien(NhanvienInfo info)
        {
            DataTable dt = new DataTable();
            dt = LayDSNhanvien();
            int i = dt.Rows.Count+1;
            string ngaysinh = info.Ngaysinh.Value.Month + "/" + info.Ngaysinh.Value.Day + "/" + info.Ngaysinh.Value.Year;
            string insertCommand = "insert into dbo.NHANVIEN values('NV" + i + "','" + info.Mataikhoan + "'," + "N'" + info.Hoten + "','"
                +ngaysinh+"','"+info.Cmnd+"',"+"N'"+info.Chucvu+"',"+"N'"+info.Phongban
                +"','"+ info.Dienthoai+"',"+"N'"+info.Diachi+"','"+info.Gioitinh+"')";
            _provider.executeNonQuery(insertCommand);
        }
        public void updatenhanvien(NhanvienInfo info)
        {
            string ngaysinh = info.Ngaysinh.Value.Month + "/" + info.Ngaysinh.Value.Day + "/" + info.Ngaysinh.Value.Year;
            string updateCommand = "update dbo.NHANVIEN set MATAIKHOAN = '"+info.Mataikhoan+"', HOTENNV = N'"+info.Hoten+"',NGSINHNV = '"+ngaysinh
                +"',CMND_NV = '"+info.Cmnd+"',CHUCVU = N'"+info.Chucvu+"',PHONGBAN = N'"+info.Phongban
                +"',DIENTHOAINV = '"+info.Dienthoai+"',DIACHINV = N'"+info.Diachi
                +"',GIOITINHNV = '"+info.Gioitinh+"' where MANV = '"+info.Manv+"'";
            _provider.executeNonQuery(updateCommand);
        }
        public DataTable LayDSNhanvien()
        {
            return _provider.GetData("select [MANV],NHANVIEN.[MATAIKHOAN],[HOTENNV],[NGSINHNV],[CMND_NV],[CHUCVU],[PHONGBAN],[DIENTHOAINV],[DIACHINV],[GIOITINHNV], MATKHAU from NHANVIEN , PHANQUYEN where NHANVIEN.MATAIKHOAN = PHANQUYEN.MATAIKHOAN order by MANV desc");
        }
        public DataTable NhanvienDangnhap()
        {
            return _provider.GetData("Select MANV from dbo.NHANVIEN where MATAIKHOAN = '" + FrmDangNhap.txt1 + "'");
        }
        public DataTable timnv()
        {
            return _provider.GetData("select MANV, NHANVIEN.MATAIKHOAN, HOTENNV,NGSINHNV,CHUCVU,CMND_NV,PHONGBAN,DIACHINV,GIOITINHNV,DIENTHOAINV,MATKHAU from NHANVIEN,PHANQUYEN where NHANVIEN.MATAIKHOAN = PHANQUYEN.MATAIKHOAN and HOTENNV like '" + frmNhanVien.timnv + "%'");
        }
        public DataTable ttnv()
        {
            return _provider.GetData("select * from NHANVIEN where MATAIKHOAN = '" + FrmDangNhap.matknv + "'");
        }
    }
}
